home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18231 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  7.4 KB

  1. Path: ix.netcom.com!news
  2. From: Bradd W. Szonye <bradds@ix.netcom.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: RE: Would/Won't you use a garbage collector?
  5. Date: 19 Apr 1996 08:40:41 GMT
  6. Organization: Netcom
  7. Message-ID: <01bb2dcc.2dbc15e0$c6c2b7c7@Zany.localhost>
  8. References: <4kamie$e4d@dfw-ixnews3.ix.netcom.com> <4kg6co$2h3@ubszh.fh.zh.ubs.com>
  9. NNTP-Posting-Host: det-mi6-06.ix.netcom.com
  10. X-NETCOM-Date: Fri Apr 19  3:40:41 AM CDT 1996
  11. X-Newsreader: Microsoft Internet News
  12.  
  13.  
  14. On Wednesday, April 10, 1996, Ian Johnston (by ubsswop) wrote...
  15. > In article <4kamie$e4d@dfw-ixnews3.ix.netcom.com>,
  16. giuliano@ix.netcom.com(Giuliano Carlini) writes:
  17. > |> I'm a long time proponent of using garbage collection in C and C++
  18. > |> programs, and I'm curious:
  19. > |>     - How many others are there?
  20. > |>     - Why don't most C/C++ programmers use it?
  21. > [...]
  22. > |> What follows is my belief for why garbage collection is so little
  23. used.
  24. > |> Feel free to respond to anything I say below, but please, first
  25. respond
  26. > |> to the questions above. I believe that most people don't use garbage
  27. > |> collection because either they:
  28. > |>     - don't know what it is
  29. > |>     - don't know it can be used with C/C++.
  30. > |>     - are misinformation
  31. > |>     - are biased against it by the C/C++ culture
  32. > I think the reasons you give are correct. I also agree that for many
  33. (simple)
  34. > systems, garbage collection (GC) is a good idea.
  35.  
  36. Agreed. For certain specialized applications, GC is great. However, I'm
  37. reluctant to use it, given the "resource acquisition is initialization"
  38. idiom in C++, described by Stroustrup and implemented through constructors
  39. and destructors. This idiom is very powerful, and best of all it comes
  40. *free* with the language.
  41.  
  42. > I would have no objection to GC being the default for C++,
  43. > provided that I could switch it off and incur *minimal penalty* by
  44. > overriding the GC. That is, minimal performance penalty induced
  45. > by the garbage collector, even though it is not used. A performance
  46. > penalty equivalent to virtual vs non-virtual function calls would be
  47. acceptable.
  48.  
  49. Which brings me to my next point. One of the most important design
  50. criteria in C++ is that a language or library feature should impose *no*
  51. cost on a program that doesn't use it. (Stroustrup concedes that it is
  52. very difficult, although not impossible, to implement exception handling
  53. without saddling programs that don't want it.)
  54.  
  55. > Remember too, that GC in C or C++ is *hard*. C has been around
  56. > for 25 years or so, and C++ for about 15; it is only relatively recently
  57. that
  58. > efficient garbage collectors have appeared for C and/or C++. It is not
  59. so much
  60. > that the culture set out biased against GC, but the culture has probably
  61. > grown that way, for the first of the three reasons you give above.
  62.  
  63. Garbage collection isn't hard. It's been around longer than C (at least as
  64. long as LISP, of the FORTRAN era), and good algorithms appeared in the
  65. 60's and 70's, even better ones recently. It isn't GC that's hard, it's
  66. finding the pointers. LISP makes it easy. C/C++ makes it darn near
  67. impossible without bulking out pointers or doing weird runtime data
  68. analyses. (By the way, I don't think many C++ programmers are unaware that
  69. GC exists. They just avoid it like the plague since naive GC routines--a
  70. la VIC-20 BASIC--can be *very* slow).
  71.  
  72. > [...]
  73.  
  74. > Third, in the code I write, I use a variety of helper classes to help
  75. manage
  76. > memory (and other resources; see below). I don't tend to use C-style
  77. arrays
  78. > (stack or heap-based). I don't tend to use raw C++ pointers. These
  79. things
  80. > dramatically reduce the potential for bugs. In addition, I make frequent
  81. use
  82. > of customised memory allocators to increase performance of
  83. allocating/releasing
  84. > space for objects. Sometimes I use shared memory. Sometimes I use
  85. statically
  86. > allocated memory. Sometimes I use heap memory.
  87.  
  88. Sounds like you program the same way I do.
  89.  
  90. > Unfortunately, there are leaks in the C libraries I use. If a 100%
  91. reliable
  92. > GC could somehow be confined to the library, that would make life
  93. easier. As it
  94. > is, I have to pick and choose the C library routines I use. Sadly, some
  95. can't
  96. > be avoided. This is an argument for GC, rather than against :-)
  97.  
  98. Fortunately this becomes less of a problem as vendors become "freer" with
  99. their source code; frequently you can fix bugs before the next release of
  100. the library. And fixing bugs is better than relying on another 3rd-party
  101. tool to provide GC. I always trust my *own* code first. (I risk sounding
  102. paranoid).
  103.  
  104. > Fourth, the code I write lives in a mixed-language environment...
  105.  
  106. Yuck. Forget about just about any advanced language feature. This makes
  107. exceptions, classes, static objects, whatever-you-like difficult.
  108.  
  109. > These are the practical reasons. There is another, major reason which is
  110. > partly practical and partly philosphical...
  111.  
  112. This is the most important point, I think.
  113.  
  114. > [much good stuff deleted to save you from reading it 2-3 more times]
  115. > [be sure to read the original to learn about resource acquisition]
  116.  
  117. > Ian
  118.  
  119. Stroustrup does an excellent if brief discussion of GC and resource
  120. acquisition/release in "The Design and Evolution of C++" if my memory
  121. serves me. I'll paraphrase him and add my own opinions to say that GC is,
  122. in *general* inappropriate for large C++ systems. The trouble with GC and
  123. C++ is there are lots of situations in C++ where you can't use it.
  124.  
  125. Take, for instance, just about anything that isn't memory-related. Many
  126. classes (such as almost all of iostreams) does something important in the
  127. destructor. In the case of fstreams, it's closing a file, which is
  128. something you usually want done now, not eventually. In the case of most
  129. of the Microsoft Foundation Classes (MFC) or Object Windows Library (OWL),
  130. a lot of destructors affect what you see on the screen. Those destructors
  131. need calling at a known time for heap objects.
  132.  
  133. On the other hand, stack objects are very good at managing memory and
  134. other resources. The only pointers you really need to use in C/C++ on a
  135. regular basis are for strings, lists, and trees. The C++ standard is due
  136. to go to ISO in July, and around that time we'll have classes to manage
  137. strings, lists, and trees. Meaning you can hide those pointers in standard
  138. classes. Teach someone to use the C++ library and they won't need to worry
  139. about pointers a lot, only iterators. Iterators are generic pointers
  140. without the related resource headaches, since the classes that generate
  141. them do the resource management. You can do this in your own code by
  142. newing your pointers in constructors and deleting them in the
  143. destructors.
  144.  
  145. So there's a couple problems or a couple solutions/alternatives to GC. Its
  146. utility is fairly limited in C++, and the language provides a built-in
  147. alternative to GC. A little discipline and a little help from the library
  148. can minimize the opportunities for memory leaks to occur.
  149.  
  150. Ian brought up the biggest problem with GC: given all the places where GC
  151. is unusable (important destructors) or unnecessary (stack objects), it's
  152. too easy to be careless with GC in the places where it does count. How
  153. easy is it to get in the habit of "The GC system will clean it up"?
  154. Nowadays memory isn't the most limited resource, especially if you're
  155. still using Windows 3.1; if you depend on GC to clean up your memory
  156. garbage, you're likely to be careless about your other garbage. Garbage is
  157. garbage, and GIGO is no longer an accepted computing paradigm. Leave GC to
  158. the languages that hide pointers and cleanup from the user entirely, like
  159. LISP and BASIC.
  160.  
  161. Bradd W. Szonye
  162.  
  163.  
  164.